package com.wzj.uarmor.utils;


import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.wzj.uarmor.entiy.Remote;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
public class ConnectLinuxUtil {
    private static int CONNECT_TIMEOUT = 30000;

    /**
     * 创建远程连接
     * @param remote
     * @return
     * @throws JSchException
     */
    public static Session getSession(Remote remote) throws JSchException {
        JSch jSch = new JSch();
        if (Files.exists(Paths.get(remote.getIdentity()))) {
            jSch.addIdentity(remote.getIdentity(), remote.getPassphrase());
        }
        Session session = jSch.getSession(remote.getHostUser(), remote.getHost(), remote.getPort());
        session.setPassword(remote.getHostPassword());
        session.setConfig("StrictHostKeyChecking", "no");
        session.connect(CONNECT_TIMEOUT);
        return session;
    }

    /**
     * 远程执行命令并处理数据
     * @param session
     * @param command
     * @return
     * @throws JSchException
     */
    public static List<Map> remoteExecute(Session session, String command) throws JSchException {
        System.out.println("输入命令 >> " + command);
        List<Map> resultLines = new ArrayList<>();
        ChannelExec channel = null;
        try {
            channel = (ChannelExec) session.openChannel("exec");
            channel.setCommand(command);
            InputStream input = channel.getInputStream();
            channel.connect(CONNECT_TIMEOUT);
            System.out.println("channel数据>> " + channel);
            try {
                BufferedReader inputReader = new BufferedReader(new InputStreamReader(input));
                System.out.println("inputReader数据>> " + inputReader);
                resultLines =TextUtil.readText2Map(inputReader);
            }
            finally {
                if (input != null) {
                    try {
                        input.close();
                    } catch (Exception e) {
                        log.error("JSch inputStream close error:", e);
                    }
                }
            }
        } catch (IOException e) {
            log.error("IOcxecption:", e);
        } finally {
            if (channel != null) {
                try {
                    channel.disconnect();
                } catch (Exception e) {
                    log.error("JSch channel disconnect error:", e);
                }
            }
        }
        return resultLines;
    }

    /**
     * 当前环境执行
     * @param cmd
     * @return
     * @throws IOException
     */
   public  static  List<Map>  ProcessExecute(String cmd) throws IOException {
       InputStream in = null;
       Process pro = Runtime.getRuntime().exec(cmd);
       List<Map> resultLines = new ArrayList<>();
       in=pro.getInputStream();
       if (in!=null){
           BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
           resultLines =TextUtil.readText2Map(bufferedReader);
       }
            return  resultLines;
   }

}
